//
//  LoginViewController.m
//  SmartCup
//
//  Created by Kendama on 2017/5/15.
//  Copyright © 2017年 DT. All rights reserved.
//

#import "LoginViewController.h"
#import "PublicLoginApi.h"
#import "PublicGetUserInfoApi.h"


@interface LoginViewController ()

@property (nonatomic, strong) UITextField *phoneTF;
@property (nonatomic, strong) UITextField *passwordTF;
@property (nonatomic, strong) UIButton *loginButton;
@property (nonatomic, strong) UIButton *registerButton;
@property (nonatomic, strong) UIButton *forgetButton;
@property (nonatomic, strong) UILabel *phoneLabel;
@property (nonatomic, strong) UILabel *passwordLabel;
@property (nonatomic, strong) UIView *line1;
@property (nonatomic, strong) UIView *line2;
@property (nonatomic, strong) UIButton *secretButton;
/** <#Description#> **/
@property(nonatomic,strong) ForgetPasswordViewController *forgetVC;
@property(nonatomic,strong) RegisterViewController *registerVC;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];

   
}

- (void)viewDidAppear:(BOOL)animated {

}

- (void)refreshBaseControlValue
{

    [self setNavigationTitle:@"登录" titleColor:[UIColor blackColor]];
    self.navigationBar.backgroundColor = [UIColor colorWithHexString:@"D0D0D0"];

    [self masLayoutSubviews];
}


#pragma mark -- 点击事件
- (void)didClickSecretButton {
    
    self.secretButton.selected = !self.secretButton.selected;
    self.passwordTF.secureTextEntry = !self.secretButton.selected;
}

- (void)didClickForgetPasswordButton {
    __weak typeof(self) weakSelf = self;
    self.forgetVC.updateBlock = ^(NSString *phone) {
        weakSelf.phoneTF.text = phone;
    };
    [self.navigationController pushViewController:self.forgetVC animated:YES];
}

- (void)didClickLoginButton {
    
    if (_phoneTF.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入手机号码"];
        return;
    }
    
    if (_passwordTF.text.length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入密码"];
        return;
    }
    
    if (![_phoneTF.text isMobileNumber]) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号码.."];
        return;
    }
    
    if (![_passwordTF.text isRegexPassword]) {
        [SVProgressHUD showErrorWithStatus:@"请输入6-20位字母或数字密码\n不包含除_@.之外的特殊符号"];
        return;
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [SVProgressHUD showInfoWithStatus:@"正在登录..."];
    });
    
    [self loginActionStart];
}

- (void)loginActionStart {
    
    [[DTCloudManager defaultJNI_iOS_SDK]loginWithUsername:self.phoneTF.text password:self.passwordTF.text successCallback:^(NSDictionary *dic) {
//        SAVE_BOOL(YES, LOGIN);
//        PublicLoginApi *api = [[PublicLoginApi alloc]initWithDic:@{@"username":self.phoneTF.text,@"password":self.passwordTF.text}];
//        [api dtDefaultInterceptStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
//            NSDictionary *dict = [request responseJSONObject];
            if ([dic[@"errcode"] intValue] == 0) {
    
                [SVProgressHUD showSuccessWithStatus:@"您已安全登录！"];
                DTCSAVE_OBJECT(self.phoneTF.text , USERNAME);
                DTCSAVE_OBJECT(self.passwordTF.text, PASSWORD);
                DTCSAVE_OBJECT(dic[@"data"][@"uid"], UID);
                DTCSAVE_OBJECT(dic[@"data"][@"token"], TOKEN);
                DTCSAVE_OBJECT(dic[@"data"][@"image"], IMAGE);
                DTCSAVE_OBJECT(@"YES", @"Login");   // 登录状态保存来设置根控制器
                DTCSAVE_OBJECT(@"YES", LOGIN);
                NSLog(@"token:%@和%@",dic[@"data"][@"token"],DTCEXRACT_OBJECT(TOKEN));
                [self GetUserInfo];
            } else {
                [SVProgressHUD showInfoWithStatus:dic[@"errmsg"]];
            }
//        } failure:nil];
    } errorCallback:^(NSDictionary *dic) {
        
        [SVProgressHUD showErrorWithStatus:dic[@"errmsg"]];
        
    }];


    
}

- (void)GetUserInfo {
    PublicGetUserInfoApi * userInfoApi = [[PublicGetUserInfoApi alloc] init];
    [userInfoApi dtDefaultInterceptStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        [userInfoApi analyseResponseObject];
        DTCSAVE_OBJECT(@"YES", LOGIN);
        [[AppDelegate defaultService]setRootViewController];
    } failure:nil];
}


- (void)didClickRegisterButton {
    __weak typeof(self) weakSelf = self;
    self.registerVC.updateBlock = ^(NSString *phone) {
        weakSelf.phoneTF.text = phone;
    };
    [self.navigationController pushViewController:self.registerVC animated:YES];
}


#pragma mark -- 懒加载
- (UITextField *)phoneTF
{
    if(!_phoneTF)
    {
        _phoneTF = [[UITextField alloc]init];
        _phoneTF.placeholder = @"Phone Number";
        _phoneTF.keyboardType = UIKeyboardTypePhonePad;
        [_phoneTF setFont:[UIFont systemFontOfSize:px1080Width(45)]];
        [self.view addSubview:_phoneTF];
    }
    return _phoneTF;
}


- (UITextField *)passwordTF
{
    if(!_passwordTF)
    {
        _passwordTF = [[UITextField alloc]init];
        _passwordTF.placeholder = @"Password";
        _passwordTF.secureTextEntry = YES;
        [_passwordTF setFont:[UIFont systemFontOfSize:px1080Width(45)]];
        [self.view addSubview:_passwordTF];
    }
    return _passwordTF;
}

- (UIView *)line1
{
    if(!_line1)
    {
        _line1 = [[UIView alloc]init];
        _line1.backgroundColor = [UIColor colorWithHexString:@"#C9C9C9"];
        [self.view addSubview:_line1];
    }
    return _line1;
}
- (UIView *)line2
{
    if(!_line2)
    {
        _line2 = [[UIView alloc]init];
        _line2.backgroundColor = [UIColor colorWithHexString:@"#C9C9C9"];
        [self.view addSubview:_line2];

    }
    return _line2;
}


- (UIButton *)secretButton
{
    if(!_secretButton)
    {
        _secretButton = [[UIButton alloc]init];
        [_secretButton setImage:[UIImage imageNamed:@"icon_hide"] forState:UIControlStateNormal];
        [_secretButton setImage:[UIImage imageNamed:@"icon_visual"] forState:UIControlStateSelected];
        [_secretButton addTarget:self action:@selector(didClickSecretButton) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_secretButton];
    }
    return _secretButton;
}

- (UIButton *)loginButton
{
    if(!_loginButton)
    {
        _loginButton = [[UIButton alloc]init];
        [_loginButton setTitle:@"登录" forState:UIControlStateNormal];
//        [_loginButton.titleLabel setFont:<#(UIFont * _Nullable)#>];
        [_loginButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_loginButton addTarget:self action:@selector(didClickLoginButton) forControlEvents:UIControlEventTouchUpInside];
        _loginButton.layer.cornerRadius = 20;
        _loginButton.backgroundColor = [UIColor colorWithHexString:@"#D0D0D0"];
        [self.view addSubview:_loginButton];
    }
    return _loginButton;
}


- (UIButton *)registerButton
{
    if(!_registerButton)
    {
        _registerButton = [[UIButton alloc]init];
        [_registerButton setTitle:@"立即注册" forState:UIControlStateNormal];
        [_registerButton setTitleColor:[UIColor colorWithHexString:@"#B2B2B2"] forState:UIControlStateNormal];
        [_registerButton.titleLabel setFont:[UIFont systemFontOfSize:px1080Width(45)]];
        [_registerButton addTarget:self action:@selector(didClickRegisterButton) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_registerButton];
    }
    return _registerButton;
}


- (UIButton *)forgetButton
{
    if(!_forgetButton)
    {
        _forgetButton = [[UIButton alloc]init];
        [_forgetButton setTitle:@"忘记密码？" forState:UIControlStateNormal];
        [_forgetButton setTitleColor:[UIColor colorWithHexString:@"#B2B2B2"] forState:UIControlStateNormal];
        [_forgetButton.titleLabel setFont:[UIFont systemFontOfSize:px1080Width(45)]];
        [_forgetButton addTarget:self action:@selector(didClickForgetPasswordButton) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:_forgetButton];
    }
    return _forgetButton;
}

- (UILabel *)phoneLabel
{
    if(!_phoneLabel)
    {
        UIView *superView = self.view;
        _phoneLabel = [[UILabel alloc]init];
        [superView addSubview:_phoneLabel];
        [_phoneLabel setFont:[UIFont systemFontOfSize:px1080Width(45)]];
        [_phoneLabel setTextColor:[UIColor colorWithHexString:@"#3C3C3C"]];
        _phoneLabel.text = @"手机号";
    }
    return _phoneLabel;
}
- (UILabel *)passwordLabel
{
    if(!_passwordLabel)
    {
        UIView *superView = self.view;
        _passwordLabel = [[UILabel alloc]init];
        [superView addSubview:_passwordLabel];
        [_passwordLabel setFont:[UIFont systemFontOfSize:px1080Width(45)]];
        [_passwordLabel setTextColor:[UIColor colorWithHexString:@"#3C3C3C"]];
        _passwordLabel.text = @"密码";
    }
    return _passwordLabel;
}

- (ForgetPasswordViewController *)forgetVC
{
    if(!_forgetVC)
    {
        _forgetVC = [[ForgetPasswordViewController alloc]init];
    }
    return _forgetVC;
}

- (RegisterViewController *)registerVC
{
    if(!_registerVC)
    {
        _registerVC = [[RegisterViewController alloc]init];
    }
    return _registerVC;
}



#pragma mark -- 初始化
- (void)masLayoutSubviews {
    __weak UIView *superView = self.view;
    CGFloat edge = px1080Width(60);
    CGFloat high = px1920Height(80);
    
    [self.line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(superView).with.offset(px1920Height(242)+64);
        make.left.equalTo(superView).with.offset(edge);
        make.right.equalTo(superView).with.offset(-edge);
        make.height.mas_equalTo(1);
    }];
    
    [self.phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.line1).with.offset(-px1920Height(16));
        make.height.mas_equalTo(high);
        make.left.equalTo(superView).with.offset(edge);
        make.right.equalTo(superView).with.offset(-edge);
    }];
    
    
    [self.phoneLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.phoneTF.mas_top).with.offset(-px1920Height(16));
        make.height.mas_equalTo(high);
        make.left.equalTo(superView).with.offset(edge);
        make.right.equalTo(superView).with.offset(-edge);
    }];
    
    
    [self.line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.line1.mas_bottom).with.offset(px1920Height(245));
        make.left.equalTo(self.view).with.offset(edge);
        make.right.equalTo(self.view).with.offset(-edge);
        make.height.mas_equalTo(1);
    }];
    
    [self.secretButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.line2).with.offset(-px1920Height(30));
        make.right.equalTo(self.view).with.offset(-2*edge);
        make.height.mas_equalTo(px1920Height(60));
        make.width.mas_equalTo(px1080Width(60));
    }];
    
    [self.passwordTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.line2).with.offset(-px1920Height(16));
        make.height.mas_equalTo(high);
        make.left.equalTo(self.view).with.offset(edge);
        make.right.equalTo(self.secretButton.mas_left).with.offset(-edge);
    }];
    
    
    [self.passwordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.passwordTF.mas_top).with.offset(-px1920Height(16));
        make.height.mas_equalTo(high);
        make.left.equalTo(superView).with.offset(edge);
        make.right.equalTo(superView).with.offset(-edge);
    }];
    
    [self.registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.line2.mas_bottom).with.offset(px1920Height(66));
        make.left.equalTo(self.view).with.offset(edge);
        make.width.mas_equalTo(px1080Width(200));
        make.height.mas_equalTo(high);
    }];
    
    [self.forgetButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.line2.mas_bottom).with.offset(px1920Height(66));
        make.right.equalTo(self.view).with.offset(-edge);
        make.width.mas_equalTo(px1080Width(240));
        make.height.mas_equalTo(high);
    }];
    
    
    [self.loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.line2.mas_bottom).with.offset(px1920Height(224));
        make.centerX.mas_equalTo(superView.mas_centerX);
        make.width.mas_equalTo(px1080Width(500));
        make.height.mas_equalTo(px1920Height(136));
    }];
    
}

@end
